// pages/roomRank/roomRank.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openpop_upWindow:false,
    time_t: null,
    type: 2, //0财富1魅力
    dateType: 1, //1日榜2周榜
    rankList1: "",
    rankList2: "",
    myInfo: {},
    img_level: "https://xunyinjiaoyou.oss-cn-qingdao.aliyuncs.com/static/level/",
    img_charm: "https://xunyinjiaoyou.oss-cn-qingdao.aliyuncs.com/static/charmlevel/",
    endTime: '',
  },

  return () {
    wx.navigateBack({
      delta: 1,
    })
  },
  typeChange1() {
    this.setData({
      type: 1
    })
    this.getRank()
  },
  typeChange2() {
    this.setData({
      type: 2
    })
    this.getRank()
  },
  typeChange3() {
    this.setData({
      type: 3
    })
    this.getRank()
  },
  dateChange(data) {
    // console.log(data);
    this.setData({
      dateType: data.currentTarget.dataset.datechange
    })
    this.getRank()
  },

  topersonpage(data) {
    // console.log(data.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/personpage/personpage?memberId=' + data.currentTarget.dataset.id,
    })
  },
  //去个人主页
  toperson(data) {
    wx.navigateTo({
      url: '/pages/me/me?memberId=' + data.currentTarget.dataset.item_s,
    })
  },
  getRank() {
    let url = '';
    if (this.data.type == '1') {
      url = '/finding/starRankV2'; //魅力榜
    } else if (this.data.type == '2') {
      url = '/finding/richRankV2'; //财富榜
    } else {
      url = '/finding/room_rank'; //房间榜 
    }
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&type=" +
      this.data.dateType +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      type: this.data.dateType,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: url,
      data: params
    }).then((res) => {
      // console.log(res.data.result)
      let that = this;
      that.setData({
        rankList1: res.data.result.lastList,
        rankList2: res.data.result.list,
        myInfo: res.data.result.myInfo,
      })
      // clearInterval(c);
      // let time_ = res.data.result.endTime;
      // var c = setInterval(() => {
      //   that.setData({
      //     time_: time_--
      //   })
      that.minReturn(res.data.result.endTime)
      // }, 1000);
    })
  },
  formatBit (val) {
    val = +val
    return val > 9 ? val : '0' + val
  },
　　 // 秒转时分秒，求模很重要，数字的下舍入
  formatSeconds (time) {
    let min = Math.floor(time % 3600)
    let val = this.formatBit(Math.floor(time / 3600)) + '时' + this.formatBit(Math.floor(min / 60)) + '分' + this.formatBit(time % 60)+ '秒' 
    return val
  },
　　 // 定时器
  minReturn (time_) {
    let time = time_
    clearInterval(this.time_t)
    this.time_t = setInterval(() => {
      time--
     this.formatSeconds(time)
     this.setData({
      endTime:this.formatSeconds(time)
     })
      if (time <= 0) {
        clearInterval(this.time_t)
      }
    }, 1000)
  },
  openpop_upWindow(){
    this.setData({
      openpop_upWindow:!this.data.openpop_upWindow
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getRank()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
